package com.hz.cheetah.config.spring.schema;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;

/**
 * Created by yangchuan on 16/6/15.
 */
public class DynamicBeanDefinitionParser extends AbstractSingleBeanDefinitionParser /*implements*/ /*BeanDefinitionParser*/ {

    private final Class<?> beanClass;

    private final boolean required;

    public DynamicBeanDefinitionParser(Class<?> beanClass, boolean required) {
        this.beanClass = beanClass;
        this.required = required;
    }

    protected Class getBeanClass(Element element) {
        return beanClass;
    }

    protected void doParse(Element element, BeanDefinitionBuilder bean) {
        String interfaceName = element.getAttribute("interface");
        Class<?> interfaceClass = null;
        try {
            interfaceClass = Class.forName(interfaceName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        bean.addPropertyValue("interfaceClass",interfaceClass);
        String version = element.getAttribute("version");
        if(version.equals("0.0.0")){
            version = "";
        }
        bean.addPropertyValue("version",version);
        bean.addPropertyValue("factory",element.getAttribute("factory"));
    }

   /* @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClass(beanClass);
        beanDefinition.setLazyInit(false);
        beanDefinition.getPropertyValues().add("id", element.getAttribute("id"));
        String interfaceName = element.getAttribute("interface");
        Class<?> interfaceClass = null;
        try {
            interfaceClass = Class.forName(interfaceName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        beanDefinition.getPropertyValues().add("interfaceName",interfaceName);
        beanDefinition.getPropertyValues().add("interfaceClass",interfaceClass);
        beanDefinition.getPropertyValues().add("version",element.getAttribute("version"));
        return beanDefinition;
    }*/
}
